# import random

# import matplotlib.pyplot as plt
# import networkx as nx

# # Set the random seed for reproducibility
# random_seed = 32
# random.seed(random_seed)

# G = nx.erdos_renyi_graph(10, 0.3)

# nx.draw_networkx(G)
from udpCoapServer import *
import os

def baotouTest():
    data = 'F88F003001707910002000300023120512124802003A00000000000001C801C603003901000623120512001102000200050300011F040002FDD905000200720600010007000104080002015D0A000217730B000842F2869D41F729440701842312031515000000050E0301437995E2435538A0434F133B43305840432B7AA6438B8489431F716A436F15FF41AD590D4374F9D642F7B1AA43048DCF433783E1436D4B170102009F04C9009F04DC009F04ED009F04FA009F0508009F0518009F0527009F0537009F0546009F0553009F0563009F0574009F0583009F059903033F649BA63F7810623F19DB233DBA5E353F3B645A3E9E353F3F49BA5E3F12F1AA3E71A9FC3F476C8B3E5E353F3DF5C28F3EC083123C9BA5E301040000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F0000022F03053E1168733E1168733E1168733E1168733E1168733E1168733E1168733E1168733E8000003E8000003E8000003E8000003E8000003E800000030600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000407000000000000000000000000000000000000000000000000000000000600003716'
    
    # 此报文中压力解析有问题
    data = 'F88F002000000410478200200023120912020302007300000000000001C801C603003901000623120815470302000200010300011B040002FD4E050002008D0600010007000104080002016F0A00021E170B000842F2869D41F729440701842312091147000000010E0301452EC02F452EB7C3452EE599452EBF7F452EDB38452EEDFC452EC49D452EC862452EC2E8452EBC85452EC90B452EB77A452EC586452EC67F010202B61F0F02B61F3E02B61F6C02B61F9B02B61FC902B61FF802B6202702B6205502B6208402B620B202B620E102B6211002B6213E02B6216D03033F178D503E4083123F4AC0833ECAC0833F6083123F0000003E1168733F3DF3B63EAF1AA03F71A9FC3F0B851F3E0F5C293F3DB22D3EAE978D01040008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D0008242D03053F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED913F1CED910306510041F746CB41F97B9941F84F9F41F86FA541FA854241F9559A41F95A2841F853A142014C4641FB5AB441FB467741F8568F41FB57F041F7040720002000200020002000200020002000200020002000200020002000060000F116'
    
    retList = BaoTouShuiwuProto(data) 
    if retList != None:
        for index,value in enumerate(retList[0]):
            # log.logger.info("-->%s[thID:%s]:%s", self.thName, self.thIdent, value)
            print(value)
            pass
    
        # 写入数据库
        # sqliteHandle("./meterFlow.db", retList[1])
def test():
    import requests
    from bs4 import BeautifulSoup

    # 定义要打开的网页URL
    url = 'https://www.rsj.sh.gov.cn/zcps/zcpssb/listServlet?listName=gs'

    headers = {        
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
    }
    # 发送GET请求打开网页
    response = requests.get(url,headers=headers)

    # 使用BeautifulSoup解析网页HTML代码
    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找要输入数字的输入框元素
    # <input height="80%" size="1" 
    # id="pageSize" name="pageSize" value="10" class="input_txtPage" onchange="turnToPage('1');" maxlength="3">
    input_box = soup.find('input', {'id': 'pageSize'})

    # 在输入框中输入数字
    input_box.text = 100

    # 模拟回车操作
    input_box.attrs['value'] = input_box.attrs['value'] + '\n'

    # 打印修改后的HTML代码，可以看到输入的数字已经被插入到网页中
    print(soup.prettify())

def tcpPortCheck(server):
    import socket
    try:        
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        sock.connect(server)
        sock.close()        
        return True
    except socket.error as e:
        print('connect {} error'.format(server))
        return False
    finally:
        sock.close()
    
    
if __name__ == '__main__':
    # baotouTest()
    # test()
    # server = ('61.139.65.142', 31200)
    server = ('172.16.0.170', 9425)
    if tcpPortCheck(server) == True:
        print('connect server {} success!'.format(server))
    else:
        print('connect server {} error!'.format(server))
    
    # dbDir = './dbDir/' #需要创建的文件夹
    # if not os.path.exists(dbDir):
    #     os.mkdir(dbDir)
    #     # os.makedirs(dbDir)

    # savename = dbDir + '123.txt'
    # with open(savename, 'w') as fp:
    #     fp.write("123456")
    
    # try:
    #     with open('filename.txt', 'r') as file:
    #         content = file.read()
    #         # 其他操作
    # # except IOError as e:
    # #     print(f"发生了IO错误: {e}")
    # except Exception as e:
    #     print(f"发生了其他异常: {e}")
            
    
    
    